# -*- coding: iso8859-1 -*-
#
# Copyright (C) 2005 CSIRO Australia
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.
#
# This software consists of voluntary contributions made by many
# individuals. For exact contribution history, see the revision
# history and logs.
#
# Author: Conrad Parker <conrad@annodex.net>

import commands
import re

class OggzInfo (object):

  def __init__(self, filename):
    self.output = commands.getoutput ("oggz-info -b \"%s\"" % (filename))

  def headerval (self, field):
      pattern = '%s: (.*)' % (field)
      match = re.search (pattern, self.output)
      if match:
        return match.group(1)
      else:
        return "Unknown"

  def duration(self):
    return self.headerval ('^Content-Duration')

  def bitrate(self):
    return self.headerval ('Content-Bitrate-Average')

  def resolution(self):
    width = self.headerval ('Video-Width')
    height = self.headerval ('Video-Height')
    return '%sx%s' % (width, height)

  def framerate(self):
    return self.headerval ('Video-Framerate')

  def has_video(self):
    return (self.framerate() != "Unknown")

  def has_audio(self):
    samplerate = self.headerval ('Audio-Samplerate')
    return (samplerate != "Unknown")

  def audio(self):
    samplerate = self.headerval ('Audio-Samplerate')
    if (samplerate == "Unknown"):
      return "None"

    channels = self.headerval ('Audio-Channels')
    if (channels == "1"):
      cdesc= "Mono"
    elif (channels == "2"):
      cdesc= "Stereo"
    else:
      cdesc= "%s channel" % (channels)

    return '%s %s' % (cdesc, samplerate)
